package com.efast.dmom.mes.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WmsInWarehouseOrder;
import com.efast.dmom.mes.service.WmsInWarehouseOrderService;

@Controller
@RequestMapping("/wmsInWarehouseOrder")
public class WmsInWarehouseOrderController {
	
	@Autowired
	private WmsInWarehouseOrderService wmsInWarehouseOrderService;
	@RequestMapping("/editOrAdd")
	public @ResponseBody ResultBean purchaseOrderSaveOrEdit (@RequestBody List<WmsInWarehouseOrder> wmsInWarehouseOrder) {
		if (wmsInWarehouseOrder.get(0).getFlag().equals("add")) {
			ResultBean save = wmsInWarehouseOrderService.batchSave(wmsInWarehouseOrder);
			return save;
		} else if (wmsInWarehouseOrder.get(0).getFlag().equals("edit")) {
			ResultBean update = wmsInWarehouseOrderService.update(wmsInWarehouseOrder.get(0));
			return update;
		}
		return null;
	}
	@RequestMapping("/confirmOrBatchDelete")
	public @ResponseBody ResultBean confirmOrBatchDelete (@RequestBody List<WmsInWarehouseOrder> wmsInWarehouseOrder) {
		if (wmsInWarehouseOrder.get(0).getFlag().equals("batchConfirm")) {
			ResultBean batchConfirm = new ResultBean(true);
			try {
				wmsInWarehouseOrderService.batchConfirm(wmsInWarehouseOrder);
			} catch (RuntimeException e) {
				batchConfirm.setErrorMessage(e.getMessage());
				return batchConfirm;
			}
			return batchConfirm;
		} else if (wmsInWarehouseOrder.get(0).getFlag().equals("batchDelete")) {
			ResultBean batchDelete = wmsInWarehouseOrderService.deleteByNos(wmsInWarehouseOrder);
			return batchDelete;
		}
		return null;
	}
	
	@RequestMapping("/itemSaveOrEdit")
	public @ResponseBody ResultBean saveOrEdit (@RequestBody WmsInWarehouseOrder wmsInWarehouseOrder) {
		if (wmsInWarehouseOrder.getFlag().equals("add")) {
			ResultBean save = wmsInWarehouseOrderService.save(wmsInWarehouseOrder);
			return save;
		} else if (wmsInWarehouseOrder.getFlag().equals("edit")) {
			ResultBean update = wmsInWarehouseOrderService.update(wmsInWarehouseOrder);
			return update;
		}
		return null;
		
	}
	
	@RequestMapping("/delete")
	public @ResponseBody ResultBean delete (@RequestBody List<WmsInWarehouseOrder> list) {
		if (list.get(0).getFlag().equals("delete")) {
			ResultBean save = wmsInWarehouseOrderService.batchDelete(list);
			return save;
		}
		
		return null;
		
	}

}
